using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using JlPay.Sdk.Models;
using JlPay.Sdk.Services;

namespace JlPay.Sdk.Models.Openmerch.Query
{
    /// <summary>
    /// 实名认证查询响应
    /// </summary>
    public class RealNameQueryResponse : StandardResponse, IComplexFieldHandler
    {
        /// <summary>
        /// 实名认证信息列表
        /// </summary>
        [JsonPropertyName("realname_info")]
        public List<RealNameInfo>? RealnameInfo { get; set; }

        /// <summary>
        /// 处理复杂字段
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="fieldValue">字段值</param>
        public void HandleComplexField(string fieldName, object fieldValue)
        {
            if (fieldName == "realname_info")
            {
                if (fieldValue is JsonElement jsonElement)
                {
                    if (jsonElement.ValueKind == JsonValueKind.Array)
                    {
                        RealnameInfo = new List<RealNameInfo>();
                        foreach (var item in jsonElement.EnumerateArray())
                        {
                            if (item.ValueKind == JsonValueKind.Object)
                            {
                                var realNameInfo = JsonSerializer.Deserialize<RealNameInfo>(item.GetRawText(), new JsonSerializerOptions 
                                { 
                                    PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower 
                                });
                                if (realNameInfo != null)
                                {
                                    RealnameInfo.Add(realNameInfo);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 实名认证信息
    /// </summary>
    public class RealNameInfo
    {
        /// <summary>
        /// 申请状态，1-审核中，2-待确认联系信息，3-待账户验证，4-审核通过，5-审核驳回，6-已冻结，7-已作废（query_type=0时返回）
        /// </summary>
        [JsonPropertyName("applyment_status")]
        public string? ApplymentStatus { get; set; }

        /// <summary>
        /// 二维码链接地址，二维码url地址（微信二维码有效期30分钟）
        /// </summary>
        [JsonPropertyName("qrcode_data")]
        public string? QrcodeData { get; set; }

        /// <summary>
        /// 驳回字段，驳回的字段名
        /// </summary>
        [JsonPropertyName("reject_param")]
        public string? RejectParam { get; set; }

        /// <summary>
        /// 审核驳回原因，实名认证驳回原因
        /// </summary>
        [JsonPropertyName("reject_reason")]
        public string? RejectReason { get; set; }

        /// <summary>
        /// 授权状态，200-已授权，201-未授权（query_type=1时返回）
        /// </summary>
        [JsonPropertyName("authorize_state")]
        public string? AuthorizeState { get; set; }
    }
} 